<?php
namespace Common\Model;

use Think\Model;

class CrmActivityModel extends Model
{
    protected $_validate = array(
        array('info', 'require', '记录不能为空', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
//        array('tel', '\d{3}-\d{8}|\d{4}-\d{7}', '电话号码错误', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
    );
    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('state', '0', self::MODEL_BOTH),
    );

    public function update($data)
    {
        $this->startTrans();

        if (empty($data['id'])) {
            $result = $this->add($data);
            fc_set_temp_statistics($data['add_user_id'], 'phone_count', 1);
        } else {
            $result = $this->save($data);
        }

        $contact = D('CrmCustomContact');
        $row = $contact->accumulation_follow_times($data['phone']);
//        $row = true;
        if ($result && $row) {
            $this->commit();
        } else {
            $this->rollback();
        }
        return true;
    }

    public function delete($id)
    {
        $data['state'] = -1;
        $result = $this->where('id=%d', $id)->save($data);
        if ($result) {
            return true;
        }
        return false;
    }

    public function get($id)
    {
        $condition = array();
        $condition['id'] = array('eq', $id);
        $condition['state'] = array('egt', 0);
        $model = $this->field(true)->where($condition)->find();
        return $model;
    }

    public function get_list_by_phone($phones)
    {
        $condition = array();
        $condition['state'] = array('egt', 0);
        $condition['phone'] = array('in', $phones);

        $list = $this->where($condition)->order('activity_time desc')->select();
        return $list;
    }

}